package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.PurchaseStatusConstant;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.PurchaseService;
import com.atguigu.gulimall.ware.vo.MemgetPurchaseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryunreceiveList(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryWrapper)){
            queryWrapper.and(v->{
                v.eq("status","0").or().eq("status","1");
            });
        }
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void memgerPurchase(MemgetPurchaseVo memgetPurchaseVo) {

        //采购单id为空的时候，生成一个purchase_id
        if (StringUtils.isEmpty(memgetPurchaseVo.getPurchaseId())){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setStatus(PurchaseStatusConstant.PurchaseStatusEnum.CREATED.getCode());
            this.save(purchaseEntity);
            memgetPurchaseVo.setPurchaseId(purchaseEntity.getId());
        }
        List<PurchaseDetailEntity> entityList = Arrays.stream(memgetPurchaseVo.getItems()).map(v -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(v);
            purchaseDetailEntity.setPurchaseId(memgetPurchaseVo.getPurchaseId());
            purchaseDetailEntity.setStatus(PurchaseStatusConstant.PurchaseStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        purchaseDetailService.updateBatchById(entityList);

        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(memgetPurchaseVo.getPurchaseId());
        purchaseEntity.setCreateTime(new Date());
        this.updateById(purchaseEntity);
    }

}